# -*- coding: utf-8 -*-
"""
Created on Thu Nov 09 22:22:02 2017

@author: Administrator
"""
from sqlalchemy import create_engine
import tushare as ts
import numpy as np 
import mpi4py.MPI as MPI  

comm = MPI.COMM_WORLD  
comm_rank = comm.Get_rank()  
comm_size = comm.Get_size()  

if comm_rank == 0: 
    
    data=ts.get_stock_basics()
    data=data.index

engine = create_engine('sqlite:///foo1.db')
local_data=comm.bcast(data if comm_rank == 0 else None, root=0) 
num_lines = len(local_data)  
local_lines_offset = np.linspace(0, num_lines, comm_size +1).astype('int')  
local_lines = local_data[local_lines_offset[comm_rank] :local_lines_offset[comm_rank + 1]] 

print('the rank is %d and the lines number is %d' % (comm_rank,len(local_lines)))
for j,i in enumerate(local_lines):
    print('第%d个股票，股票代码%s' % (j+1,i))
    df=ts.get_k_data(i, ktype='D', autype='qfq',start='2000-1-01')
    df.to_sql(i,engine,if_exists='append')
    
#追加数据到现有表
#df.to_sql('tick_data',engine,if_exists='append')
    
#mpi4py  
    
    
    
    
    
